import { PlusOutlined, UploadOutlined } from '@ant-design/icons';
import {
  ModalForm,
  ProForm,
  ProFormDateRangePicker,
  ProFormSelect,
  ProFormText,
  ProFormInstance,
  ProFormDatePicker,
} from '@ant-design/pro-components';
import { Button, message, Upload, Breadcrumb } from 'antd';
import type { UploadProps } from 'antd';
import React, { useEffect, useRef, useState } from 'react';
import { useRequest } from 'umi';

const waitTime = (time: number = 100) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, time);
  });
};

export default ({ tableName, Refresh }: any) => {
  const formRef = useRef<ProFormInstance>();

  const Tag =
    tableName === 'OPEN_SHARE' ||
    tableName === 'LEADER_RESEARCH' ||
    tableName === 'OPEN_TOPIC' ||
    tableName === 'VISIT_ENTERPRISE' ||
    tableName === 'HOST_UNDER_MEETING';

  const props: UploadProps = {
    name: 'file',
    action: 'http://10.1.40.84:9993/common/uploadFile',
    headers: {
      Authorization: localStorage.getItem('token') || '',
    },
    maxCount: 1,
    onChange(info) {
      if (info.file.status !== 'uploading') {
        console.log(info.file, info.fileList);
      }
      if (info.file.status === 'done') {
        formRef.current?.setFieldsValue({ fileName: info.file.response.data });
        console.log(info.file.response.data);
        message.success(`${info.file.name} 上传成功！`);
      } else if (info.file.status === 'error') {
        message.error(`${info.file.name} file upload failed.`);
      }
    },
  };

  const exportData = useRequest(
    (data) => {
      return {
        url: `http://10.1.40.84:9993/common/exportData`,
        method: 'POST',
        headers: {
          Authorization: localStorage.getItem('token') || '',
        },
        data,
      };
    },
    {
      manual: true,
    },
  );

  return (
    <ModalForm
      title="导入文件"
      trigger={<Button type="primary">导入文件</Button>}
      autoFocusFirstInput
      modalProps={{
        onCancel: () => console.log('run'),
        destroyOnClose: true,
      }}
      submitTimeout={2000}
      onFinish={async (values) => {
        await waitTime(2000);
        console.log(values);
        exportData.run(values).then(() => {
          Refresh();
        });
        message.success('提交成功');
        return true;
      }}
      formRef={formRef}
      initialValues={{ tableName: tableName }}
    >
      <ProForm.Group>
        <ProFormText name="tableName" label="tableName" hidden />
        <ProFormDatePicker.Year
          name="year"
          label="年度"
          required
          hidden={Tag}
        />
        <ProFormText width="md" name="fileName" label="文件地址" hidden />
        <Upload {...props}>
          <Button
            icon={<UploadOutlined />}
            type="link"
            style={{ marginTop: 30 }}
          >
            上传EXCEl
          </Button>
        </Upload>
        <a
          onClick={() => {
            window.open();
          }}
        ></a>
      </ProForm.Group>
    </ModalForm>
  );
};
